Imports System.Windows.Forms
Imports MatrixDataStructures

Public Class TalairachTransformationChoose
    Public OriginalAC As New Coordinate(0, 0, 0)
    Public OriginalPC As New Coordinate(0, 0, 0)
    Public AP As New Coordinate(0, 0, 0)
    Public sP As New Coordinate(0, 0, 0)
    Public pP As New Coordinate(0, 0, 0)
    Public lP As New Coordinate(0, 0, 0)
    Public rP As New Coordinate(0, 0, 0)
    Public iP As New Coordinate(0, 0, 0)
    Dim view As New PointChooseForm
    Dim currentimage As Image

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        currentimage = FormMain.AnatomyDisplay.CurrentImage(False)
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If view Is Nothing Then Exit Sub
        If view.Visible = False Then Exit Sub

        With FormMain
            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                OriginalAC.x = CShort(view.CurrentSelectedPoint.X)
                OriginalAC.y = CShort(view.CurrentSelectedPoint.Y)
                OriginalAC.z = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                OriginalAC.y = CShort(view.CurrentSelectedPoint.X)
                OriginalAC.z = CShort(view.CurrentSelectedPoint.Y)
                OriginalAC.x = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                OriginalAC.x = CShort(view.CurrentSelectedPoint.X)
                OriginalAC.z = CShort(view.CurrentSelectedPoint.Y)
                OriginalAC.y = CShort(.AnatomyDisplay.CurrentSliceIndex)
            End If
            Button1.Text = OriginalAC.x & "," & OriginalAC.y & "," & OriginalAC.z
        End With
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        'next
        If view Is Nothing Then Exit Sub
        If view.Visible = False Then Exit Sub
        view.Close()
        With FormMain

            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                If .AnatomyDisplay.CurrentSliceIndex + 1 < .AnatomyVolume.Matrix.z Then .AnatomyDisplay.ChangeCurrentSliceInfo("t", .AnatomyDisplay.CurrentSliceIndex + 1)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                If .AnatomyDisplay.CurrentSliceIndex + 1 < .AnatomyVolume.Matrix.y Then .AnatomyDisplay.ChangeCurrentSliceInfo("c", .AnatomyDisplay.CurrentSliceIndex + 1)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                If .AnatomyDisplay.CurrentSliceIndex + 1 < .AnatomyVolume.Matrix.x Then .AnatomyDisplay.ChangeCurrentSliceInfo("s", .AnatomyDisplay.CurrentSliceIndex + 1)
            End If
            Me.currentimage = New Bitmap(.AnatomyDisplay.CurrentImage(False))
        End With
        view = New PointChooseForm(currentimage)
        view.Show()
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        'previous
        If view Is Nothing Then Exit Sub
        If view.Visible = False Then Exit Sub

        view.Close()
        With FormMain
            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                If .AnatomyDisplay.CurrentSliceIndex - 1 >= 0 Then .AnatomyDisplay.ChangeCurrentSliceInfo("t", .AnatomyDisplay.CurrentSliceIndex - 1)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                If .AnatomyDisplay.CurrentSliceIndex - 1 >= 0 Then .AnatomyDisplay.ChangeCurrentSliceInfo("c", .AnatomyDisplay.CurrentSliceIndex - 1)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                If .AnatomyDisplay.CurrentSliceIndex - 1 >= 0 Then .AnatomyDisplay.ChangeCurrentSliceInfo("s", .AnatomyDisplay.CurrentSliceIndex - 1)
            End If
            Me.currentimage = New Bitmap(.AnatomyDisplay.CurrentImage(False))
        End With
        view = New PointChooseForm(currentimage)
        view.Show()
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Me.currentimage = New Bitmap(FormMain.AnatomyDisplay.CurrentImage(False))
        view = New PointChooseForm(currentimage)
        view.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If view.Visible = False Then Exit Sub
        view.Close()

        With FormMain
            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                OriginalPC.x = CShort(view.CurrentSelectedPoint.X)
                OriginalPC.y = CShort(view.CurrentSelectedPoint.Y)
                OriginalPC.z = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                OriginalPC.y = CShort(view.CurrentSelectedPoint.X)
                OriginalPC.z = CShort(view.CurrentSelectedPoint.Y)
                OriginalPC.x = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                OriginalPC.x = CShort(view.CurrentSelectedPoint.X)
                OriginalPC.z = CShort(view.CurrentSelectedPoint.Y)
                OriginalPC.y = CShort(.AnatomyDisplay.CurrentSliceIndex)
            End If
            Button2.Text = OriginalPC.x & "," & OriginalPC.y & "," & OriginalPC.z
        End With

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim x As Double = -Math.Tan((OriginalAC.y - OriginalPC.y) / ((OriginalAC.z - OriginalPC.z)))
        MsgBox("Rotating :" & x)
        FormMain.AnatomyVolume.Matrix.RotateAndTranslate(CSng(x * 180 / 3.14), 0, 0, 0, 0, 0)
        FormMain.AnatomyDisplay.OpenFile(FormMain.AnatomyVolume, "fixed", True, False)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        'ap

        If view Is Nothing Then Exit Sub
        If view.Visible = False Then Exit Sub

        With FormMain
            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                AP.x = CShort(view.CurrentSelectedPoint.X)
                AP.y = CShort(view.CurrentSelectedPoint.Y)
                AP.z = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                AP.y = CShort(view.CurrentSelectedPoint.X)
                AP.z = CShort(view.CurrentSelectedPoint.Y)
                AP.x = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                AP.x = CShort(view.CurrentSelectedPoint.X)
                AP.z = CShort(view.CurrentSelectedPoint.Y)
                AP.y = CShort(.AnatomyDisplay.CurrentSliceIndex)
            End If
            Button6.Text = AP.x & "," & AP.y & "," & AP.z
        End With
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        'pp
        If view Is Nothing Then Exit Sub
        If view.Visible = False Then Exit Sub

        With FormMain
            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                PP.x = CShort(view.CurrentSelectedPoint.X)
                PP.y = CShort(view.CurrentSelectedPoint.Y)
                PP.z = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                PP.y = CShort(view.CurrentSelectedPoint.X)
                PP.z = CShort(view.CurrentSelectedPoint.Y)
                PP.x = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                PP.x = CShort(view.CurrentSelectedPoint.X)
                PP.z = CShort(view.CurrentSelectedPoint.Y)
                pP.y = CShort(.AnatomyDisplay.CurrentSliceIndex)
            End If
            Button5.Text = pP.x & "," & pP.y & "," & pP.z
        End With
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        'sp
        If view Is Nothing Then Exit Sub
        If view.Visible = False Then Exit Sub

        With FormMain
            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                SP.x = CShort(view.CurrentSelectedPoint.X)
                SP.y = CShort(view.CurrentSelectedPoint.Y)
                SP.z = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                SP.y = CShort(view.CurrentSelectedPoint.X)
                SP.z = CShort(view.CurrentSelectedPoint.Y)
                SP.x = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                SP.x = CShort(view.CurrentSelectedPoint.X)
                SP.z = CShort(view.CurrentSelectedPoint.Y)
                sP.y = CShort(.AnatomyDisplay.CurrentSliceIndex)
            End If
            Button8.Text = sP.x & "," & sP.y & "," & sP.z
        End With
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        'ip
        If view Is Nothing Then Exit Sub
        If view.Visible = False Then Exit Sub

        With FormMain
            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                IP.x = CShort(view.CurrentSelectedPoint.X)
                IP.y = CShort(view.CurrentSelectedPoint.Y)
                IP.z = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                IP.y = CShort(view.CurrentSelectedPoint.X)
                IP.z = CShort(view.CurrentSelectedPoint.Y)
                IP.x = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                IP.x = CShort(view.CurrentSelectedPoint.X)
                IP.z = CShort(view.CurrentSelectedPoint.Y)
                iP.y = CShort(.AnatomyDisplay.CurrentSliceIndex)
            End If
            Button7.Text = iP.x & "," & iP.y & "," & iP.z
        End With
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        'rp
        If view Is Nothing Then Exit Sub
        If view.Visible = False Then Exit Sub

        With FormMain
            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                RP.x = CShort(view.CurrentSelectedPoint.X)
                RP.y = CShort(view.CurrentSelectedPoint.Y)
                RP.z = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                RP.y = CShort(view.CurrentSelectedPoint.X)
                RP.z = CShort(view.CurrentSelectedPoint.Y)
                RP.x = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                RP.x = CShort(view.CurrentSelectedPoint.X)
                RP.z = CShort(view.CurrentSelectedPoint.Y)
                rP.y = CShort(.AnatomyDisplay.CurrentSliceIndex)
            End If
            Button10.Text = rP.x & "," & rP.y & "," & rP.z
        End With
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        'lp
        If view Is Nothing Then Exit Sub
        If view.Visible = False Then Exit Sub

        With FormMain
            If FormMain.CurrentActiveDisplay = MainForm.DisplayType.Transverse Then
                LP.x = CShort(view.CurrentSelectedPoint.X)
                LP.y = CShort(view.CurrentSelectedPoint.Y)
                LP.z = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Coronal Then
                LP.y = CShort(view.CurrentSelectedPoint.X)
                LP.z = CShort(view.CurrentSelectedPoint.Y)
                LP.x = CShort(.AnatomyDisplay.CurrentSliceIndex)
            ElseIf FormMain.CurrentActiveDisplay = MainForm.DisplayType.Sagittal Then
                LP.x = CShort(view.CurrentSelectedPoint.X)
                LP.z = CShort(view.CurrentSelectedPoint.Y)
                lP.y = CShort(.AnatomyDisplay.CurrentSliceIndex)
            End If
            Button9.Text = lP.x & "," & lP.y & "," & lP.z
        End With
    End Sub
End Class
